package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/3/18 14:17
 * @description：https://leetcode-cn.com/problems/last-stone-weight/
 */
public class 最后一块石头的重量 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.lastStoneWeight(new int[]{2, 7, 4, 1, 8, 1}));
    }

    class Solution {
        public int lastStoneWeight(int[] stones) {
            PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> o2 - o1);
            for (int stone : stones) {
                queue.offer(stone);
            }
            while (queue.size() > 1) {
                int x = queue.poll();
                int y = queue.poll();
                if (x != y) {
                    queue.offer(x - y);
                }
            }
            return queue.isEmpty() ? 0 : queue.poll();
        }
    }
}
